package com.pengcheng.taskapplication.mvp.view.activity;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.pengcheng.taskapplication.R;
import com.pengcheng.taskapplication.contants.ConstantValue;
import com.pengcheng.taskapplication.model.AccountModel;
import com.pengcheng.taskapplication.model.AccountTranModel;
import com.pengcheng.taskapplication.model.AccountVerify;
import com.pengcheng.taskapplication.model.Dict;
import com.pengcheng.taskapplication.model.OpenAccountResult;
import com.pengcheng.taskapplication.mvp.presenter.UserPresenter;
import com.pengcheng.taskapplication.mvp.view.fragment.DialogFragmentFromBottom;
import com.pengcheng.taskapplication.mvp.view.fragment.OccupationsDialogFromBottom;
import com.pengcheng.taskapplication.net.ConvertUtil;
import com.pengcheng.taskapplication.net.IView;
import com.pengcheng.taskapplication.util.ToastUtils;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by ZFT on 2020/10/29.
 */

public class NoICBCCardActivity extends BaseActivity implements IView,OccupationsDialogFromBottom.OnFragmentInteractionListener{
    private UserPresenter userPresenter;
    @BindView(R.id.tv_user_name)
    TextView tv_user_name;
    @BindView(R.id.tv_id_card_num)
    TextView tv_id_card_num;
    @BindView(R.id.rl_select)
    RelativeLayout rl_select;
    @BindView(R.id.et_phone)
    EditText et_phone;
    @BindView(R.id.tv_occ)
    TextView tv_occ;
    @BindView(R.id.checkbox)
    CheckBox checkBox;
    @BindView(R.id.et_code)
    EditText et_code;
    @BindView(R.id.tv_xieyi)
    TextView tv_xieyi;
    private long from;

    int code;
    AccountModel accountModel;
    private String bankcardCode;

    OpenAccountResult openAccountResult;

    private ArrayList<Dict> arrayList=new ArrayList<>();

    @Override
    protected int layoutResID() {
        return R.layout.activity_no_icbc_layout;
    }

    @Override
    protected void initData() {
        bankcardCode=getIntent().getStringExtra("bankcardCode");
        from =getIntent().getLongExtra("from",0l);
        userPresenter=new UserPresenter(this);
        openAccountInit();
    }


    @Override
    public void onSuccess(String code, String msg, Object data, String actionType) {
        if(actionType.equals(ConstantValue.RequestKey.openAccountInit)){
            accountModel=(AccountModel)data;
            if(accountModel!=null){
                tv_user_name.setText(accountModel.getRealName());
                tv_id_card_num.setText(accountModel.getIdcardCode());
                arrayList.clear();
                arrayList.addAll(accountModel.getOccupations());
            }

        }
        if(actionType.equals(ConstantValue.RequestKey.openAccount)){
            openAccountResult=(OpenAccountResult)data;
        }
        if(actionType.equals(ConstantValue.RequestKey.openAccountVerify)){
           if(from==0){
               Intent intent = new Intent(context, ApplyActivity.class);
               startActivity(intent);
               NoICBCCardActivity.this.finish();
           }else if(from==1) {
               Intent intent = new Intent(context, MyBankList.class);
               startActivity(intent);
               NoICBCCardActivity.this.finish();
           }
        }
    }

    @Override
    public void onFailed(String code, String msg) {
        ToastUtils.showShortToast(msg);
    }
    public void openAccountInit(){
        userPresenter.openAccountInit(ConstantValue.RequestKey.openAccountInit);
    }

    public void openAccount(AccountTranModel accountTranModel){
        userPresenter.openAccount(accountTranModel,ConstantValue.RequestKey.openAccount);
    }

    public void openAccountVerify(AccountVerify accountVerify){
        userPresenter.openAccountVerify(accountVerify,ConstantValue.RequestKey.openAccountVerify);
    }

    @OnClick({R.id.img_back,R.id.step_next,R.id.go_back,R.id.rl_select,R.id.tv_sendCode,R.id.tv_xieyi})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.go_back:
                finish();
                break;
            case R.id.step_next:
                //判断是否勾选和填写验证码
                //点击了获取验证码
                if(openAccountResult!=null) {

                    String codeNum = et_code.getText().toString();

                    if (codeNum == null || codeNum.length() == 0) {
                        ToastUtils.showShortToast("请输入验证码");
                        return;
                    }
                    if (!checkBox.isChecked()) {
                        ToastUtils.showShortToast("请勾选个人银行电子账户服务协议");
                        return;
                    }
                    AccountVerify accountVerify = new AccountVerify();
                    accountVerify.setSerialCode(openAccountResult.getSerialCode());
                    accountVerify.setSmsSendCode(openAccountResult.getSmsSendCode());
                    accountVerify.setSmsCode(codeNum);
                    openAccountVerify(accountVerify);
                }

                break;
            case R.id.rl_select:
                showDialog(arrayList);
                break;
            case R.id.tv_xieyi:
                Intent intent =new Intent(context,Html5Activity.class);
                intent.putExtra("url","file:///android_asset/bank.html");
                startActivity(intent);
                break;
            case R.id.tv_sendCode:
                if(code==0){
                    ToastUtils.showShortToast("请选择职业");
                    return;
                }
                String phone=et_phone.getText().toString().trim();
                if(phone==null||phone.length()==0||phone.length()!=11){
                    ToastUtils.showShortToast("请输入正确的手机号");
                    et_phone.requestFocus();
                    return;
                }
                AccountTranModel accountTranModel =new AccountTranModel();
                accountTranModel.setIdcardFrontPath(accountModel.getIdcardFrontPath());
                accountTranModel.setIdcardBackPath(accountModel.getIdcardBackPath());
                accountTranModel.setBankcardCode(bankcardCode);
                accountTranModel.setIdcardCode(accountModel.getIdcardCode());
                accountTranModel.setName(accountModel.getRealName());
                accountTranModel.setPhone(phone);
                accountTranModel.setGender(accountModel.getGender());
                accountTranModel.setOccupation(code);
                accountTranModel.setForeverFlag(accountModel.isForeverFlag());
                accountTranModel.setValidityStart(ConvertUtil.convertValidityStart(accountModel.getValidityStart()));
                if(accountModel.getValidityEnd().equals("长期")){
                    accountTranModel.setForeverFlag(true);
                }else {
                    accountTranModel.setValidityEnd(ConvertUtil.convertValidityEnd(accountModel.getValidityEnd()));
                }
                accountTranModel.setOffice(accountModel.getOffice());
                accountTranModel.setAddr(accountModel.getAddr());
                accountTranModel.setBirthday(ConvertUtil.convertBirthDay(accountModel.getBirthday()));
                openAccount(accountTranModel);
                break;
        }
    }

    void showDialog(ArrayList<Dict> list) {

        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        Fragment prev = getSupportFragmentManager().findFragmentByTag("dialog");
        if (prev != null) {
            ft.remove(prev);
        }
        ft.addToBackStack(null);

        // Create and show the dialog.
        OccupationsDialogFromBottom newFragment = OccupationsDialogFromBottom.newInstance(list);
        newFragment.show(ft, "dialog");
    }

    @Override
    public void onCancel() {

    }

    @Override
    public void onCompete(Dict dict) {
        tv_occ.setText(dict.getName());
        code = Integer.parseInt(dict.getCode());
    }
}
